<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/5
 * Time: 21:53
 */

namespace Admin\Controller;
use Admin\Controller\CommonController;

class CourseReviewController extends CommonController
{
    public function index(){
        $map = array();
        if (I('get.keyword') != '') {
            $map['name'] = array('like', '%' . I('get.keyword') . '%');
        }

        $list = D('course_review')->getlist($map);
        if ($list){
            foreach ($list['list'] as $key=>$val){
                $list['list'][$key]['value'] = substr_replace(strip_tags(html_entity_decode($val['value'])),"......",50);
            }
        }
        $this->assign('count', $list['count']);
        $this->assign('totalPages', $list['totalPages']);// 总分页数
        $this->assign('nowPage', $list['nowPage']);// 当前页
        $this->assign('list', $list['list']);
        $this->assign('page',$list['show']);

        $this->display();
    }

    public function edit()
    {
        if (IS_POST) {
            $msg = array('errno' => 1, 'error' => '编辑失败，请重新编辑', 'url' => '');
            $data = I('post.');
            $action = I('action', '');

            if ($action == 'add') {
                unset($data['action']);
                unset($data['id']);
                $nameInfo = D('course_review')->getOne(array('name'=>$data['name']));//查看课程是否存在
                if ($nameInfo) {
                    $msg ['errno'] = 1;
                    $msg ['error'] = '课程已存在';
                    $this->ajaxReturn($msg);
                }
                $data['createtime'] = date("Y-m-d H:i:s",time());//添加时间
                $id = D('course_review')->insert($data);//添加数据
                if ($id) {
                    $msg ['errno'] = 0;
                    $msg ['error'] = '添加成功！';
                    $msg ['url'] = U('index');
                }
            } else if ($action == 'edit') {
                $data['id'] = I('id');
                $nameInfo = D('course_review')->getOne(array('name'=>$data['name'],'id'=>array('neq',$data['id'])));//查看课程是否存在
                if ($nameInfo) {
                    $msg ['errno'] = 1;
                    $msg ['error'] = '该课程已存在';
                    $this->ajaxReturn($msg);
                }
                unset($data['action']);
                D('course_review')->edit($data);//更新数据
                $msg ['errno'] = 0;
                $msg ['error'] = '编辑成功！';
                $msg ['url'] = U('index');
            } else {
                $msg ['errno'] = 1;
                $msg ['error'] = '数据异常';
                $this->ajaxReturn($msg);
            }
            $this->ajaxReturn($msg);
        } else {
            $action = I('action', '');
            if ($action == 'edit') {
                $data['id'] = I('id');
                $info = D('course_review')->getOne($data);//获取直营产品
                $this->assign('info', $info);
            }
            $this->display();
        }
    }
    public function del(){
        $ids = I('ids');
        $msg ['errno'] = 0;
        $msg ['error'] = "删除成功";
        if (is_array($ids)) {
            foreach ($ids as $id) {
                $data['id'] = $id;
                $flag = D('course_review')->del($data);
            }
        }
        if (!$flag) {
            $msg ['errno'] = 1;
            $msg ['error'] = "删除失败！";
        }

        $this->ajaxReturn($msg);
    }
}